import { Component } from '@angular/core';

import { NavController } from 'ionic-angular';
import { IBeacon } from 'ionic-native';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  nav: NavController;

  constructor(public navCtrl: NavController) {

    this.nav = navCtrl;

    // Request permission to use location on iOS
    IBeacon.requestAlwaysAuthorization();
    // create a new delegate and register it with the native layer
    let delegate = IBeacon.Delegate();

    // Subscribe to some of the delegate's event handlers
    delegate.didRangeBeaconsInRegion()
      .subscribe(
        data => console.log('didRangeBeaconsInRegion: ', data),
        error => console.error()
      );
    delegate.didStartMonitoringForRegion()
      .subscribe(
        data => console.log('didStartMonitoringForRegion: ', data),
        error => console.error()
      );
    delegate.didEnterRegion()
      .subscribe(
        data => {
          console.log('didEnterRegion: ', data);
        }
      );

    let uuid = "98CE3323-FADC-46BF-BB1C-3EAEFAABFF0F"; // 'F7826DA6-ASDF-ASDF-8024-BC5B71E0893E'
    let identifier = 'beaconOnTheMacBooksShelf';
    let major = 1;
    let minor = 5;
    let beaconRegion = IBeacon.BeaconRegion(identifier, uuid, major, minor);

    IBeacon.startMonitoringForRegion(beaconRegion)
      .then(
        () => console.log('Native layer recieved the request to monitoring'),
        error => console.error('Native layer failed to begin monitoring: ', error)
      );
  }

}
